@extends('master')

@section('加入我们', 'Page Title')

@section('sidebar')
	@parent
@endsection

@section('content')
	<div class="panel panel-info">
		<div class="panel-heading">
			<div class="panel-title">加入我们</div>
		</div>
		<div class="panel-body" >
			<form method="POST" action="{{ isset($id) ? '/admin/joins/update/' . $id : '/admin/joins/store'}}" class="form-horizontal" enctype="multipart/form-data" role="form">
				{!! csrf_field() !!}
				<fieldset>
					<input type="hidden" name="id" value="{{ $id or ''}}" />
					<div class="form-group">
						<label class="col-md-3 control-label" for="title">标题</label>
						<div class="col-md-9">
							<input id="title" name="title" type="text" placeholder="标题" class="form-control input-md" value="{{ $title or '' }}">
						
						</div>
					</div>
					<div class="form-group">
						<label class="col-md-3 control-label" for="nums">数量</label>
						<div class="col-md-9">
							<input id="nums" name="nums" type="text" placeholder="数量" class="form-control input-md" value="{{ $nums or '' }}">
						
						</div>
					</div>
					<div class="form-group">
						<label class="col-md-3 control-label" for="img">图片</label>
						<div class="col-md-9">
							<input id="img" name="img" type="file" placeholder="图片" class="input-md"">
							@if(isset($img))
								<img src="/{{ $img }}" height="100px;" style="margin-top: 10px;">
							@endif
						</div>
					</div>
					<div class="form-group">
						<label class="col-md-3 control-label" for="duty">职责</label>
						<div class="col-md-9">
						@if (isset($duty) && is_array(json_decode($duty, true)))
							@foreach( json_decode($duty, true) as $key => $d)
								@if($key == 0)
									<div class="row" style="margin: 10px; margin-left: 0;">
										<div class="col-md-11">
											<input name="duty[]" type="text" placeholder="职责" class="form-control input-md" value="{{ $d }}">
										</div>
										<img id="add-duty" class="col-md-1" src="/imgs/add.png" style="cursor: pointer;">
									</div>
								@else
									<div class="row" style="margin: 10px; margin-left: 0;">
										<div class="col-md-11">
											<input name="duty[]" type="text" placeholder="职责" class="form-control input-md" value="{{ $d }}">
										</div>
										<img class="del-duty col-md-1" src="/imgs/del.png" style="cursor: pointer;">
									</div>
								@endif

							@endforeach
						@else
							<div class="row" style="margin: 10px; margin-left: 0;">
								<div class="col-md-11">
									<input name="duty[]" type="text" placeholder="职责" class="form-control input-md" value="{{ $duty or '' }}">
								</div>
								<img id="add-duty" class="col-md-1" src="/imgs/add.png" style="cursor: pointer;">
							</div>
						@endif
							
							
						</div>

					</div>
					
					<div class="form-group">
						<label class="col-md-3 control-label" for="submit"></label>
						<div class="col-md-9">
							<button id="submit" name="submit" class="btn btn-primary">提交</button>
							<span id="errors" style="margin-left: 10%; color: red;">
								@if(count($errors) > 0)
									{{ $errors->all()[0] }}
								@endif
							</span>
						</div>
					</div>
				
				</fieldset>
			
			</form>
		</div>
	</div>
	<script>
		$('#submit').click(function(){

			var title = $('#title').val();
			var nums = $('#nums').val();
			var img = $('#img').val();
			var duty = $('input[name="duty[]"]').val();

			<?php if (isset($id)) {?>
				if (!title || !nums || !duty) {

					$('#errors').text('请将字段填写完整!');
					return false;
				}else{

					$('#errors').text('');
				}
			<?php }else{?>

				if (!title || !nums || !img || !duty) {

					$('#errors').text('请将字段填写完整!');
					return false;
				}else{

					$('#errors').text('');
				}
			<?php }?>

			
		});

		$('#add-duty').click(function(e){

			var str = '<div class="row" style="margin: 10px; margin-left: 0;">' +
							'<div class="col-md-11">' +
								'<input name="duty[]" type="text" placeholder="职责" class="form-control input-md">' +
							'</div>' +
							'<img class="del-duty col-md-1" src="/imgs/del.png" style="cursor: pointer;">' +
						'</div>';
			$(this).parent().parent().append(str);

			$('.del-duty').click(function(e){

				$(this).parent().remove();
			});
		});

		$('.del-duty').click(function(e){

			$(this).parent().remove();
		});
	</script>
@endsection